package com.tbabs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbabs.common.CustomException;
import com.tbabs.dto.DishDto;
import com.tbabs.entity.Dish;
import com.tbabs.entity.DishFlavor;
import com.tbabs.mapper.DishMapper;
import com.tbabs.service.DishFlavorService;
import com.tbabs.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    // 同时添加dish数据和对应flavor数据
    // 涉及多表所以要加事务
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品
        this.save(dishDto);

        // 获取flavor列表
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().peek((item) -> item.setDishId(dishDto.getId()))
                .collect(Collectors.toList());

        // 保存口味信息
        dishFlavorService.saveBatch(flavors);
    }

    // 根据菜品id来查询对饮菜品信息
    @Override
    public DishDto getInfoByIDWithFlavor(Long id) {
        // 查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        // 查询口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    // 更新菜品信息与口味信息
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // 更新dish基本信息
        this.updateById(dishDto);
        // 清除当前dish的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        // 加入更新后的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().peek((item) -> item.setDishId(dishDto.getId()))
                .collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    // 删除菜品信息和口味信息
    @Transactional
    @Override
    public void deleteWithFlavor(List<Long> ids) {
        // 查询菜品状态
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids)
                .eq(Dish::getStatus, 1);
        long count = this.count(dishLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("菜品正在销售中，无法删除！");
        }
        // 批量删除菜品基本信息
        this.removeByIds(ids);
        // 批量删除口味基本信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId, ids);

        dishFlavorService.remove(queryWrapper);
    }
}
